<template>
  <el-form :model="dataForm" ref="dataForm">
    <el-row :gutter="20" type="flex" justify="space-between" align="bottom">
      <el-col :span="6">
        <el-form-item label="姓名:">
          <el-input
            v-model="dataForm.realname"
            placeholder="请输入真实姓名"
            clearable
            style="width: 100%"
          />
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="性别:" prop="gender">
          <el-radio-group v-model="dataForm.gender" style="width: 100%">
            <el-radio-button label>全部</el-radio-button>
            <el-radio-button label="1">男</el-radio-button>
            <el-radio-button label="2">女</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="部门:" prop="deptId">
          <el-cascader
            style="width: 100%"
            filterable
            placeholder="点击选择部门"
            :options="deptList"
            v-model="dataForm.deptId"
            :props="deptListTreeProps"
            clearable
            change-on-select
          />
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="工种:" prop="craftName">
          <el-select
            v-model="dataForm.craftName"
            placeholder="点击选择工种"
            style="width: 100%"
            filterable
            clearable
          >
            <el-option
              v-for="item in typeworkList"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20" type="flex" justify="space-between" align="bottom">
      <el-col :span="6">
        <el-form-item label="学历:" prop="education">
          <el-select
            v-model="dataForm.education"
            placeholder="请选择学历"
            :loading="dutyLoading"
            style="width: 100%"
            filterable
            clearable
          >
            <el-option
              v-for="item in educationList"
              :key="item.label"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="职称:" prop="professionalTitle">
          <el-select
            v-model="dataForm.professionalTitle"
            placeholder="请选择职称"
            :loading="dutyLoading"
            style="width: 100%"
            filterable
            clearable
          >
            <el-option
              v-for="item in professionalTitleList"
              :key="item.label"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="政治面貌:" prop="politicCountenance">
          <el-select
            v-model="dataForm.politicCountenance"
            placeholder="请选择政治面貌"
            :loading="dutyLoading"
            style="width: 100%"
            filterable
            clearable
          >
            <el-option
              v-for="item in politicCountenanceList"
              :key="item.label"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="职务:" prop="duty">
          <el-select
            v-model="dataForm.duty"
            placeholder="请选择职务"
            :loading="dutyLoading"
            style="width: 100%"
            filterable
            clearable
          >
            <el-option
              v-for="item in dutyList"
              :key="item.label"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20" type="flex" justify="space-between" align="bottom">
      <el-col :span="6">
        <el-form-item label="身份证:">
          <el-input
            v-model="dataForm.identityCard"
            placeholder="请输入身份证号码"
            clearable
            style="width: 100%"
          />
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="手机号:">
          <el-input
            type="number"
            v-model="dataForm.mobile"
            placeholder="请输入手机号"
            clearable
            :min="1"
            style="width: 100%"
          />
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="年龄:">
          <el-input-number
            controls-position="right"
            v-model="dataForm.age"
            placeholder="年龄以下"
            clearable
            :min="1"
            style="width: 100%"
          />
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="员工编号:">
          <el-input v-model="dataForm.jobNumber" placeholder="员工编号" clearable style="width: 100%" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20" type="flex" justify="end" align="bottom">
      <el-col :span="6" v-if="scoreType">
        <el-form-item label="状态:" prop="scoreType">
          <el-radio-group v-model="otherData.scoreType" style="width: 100%" @change="search(1)">
            <el-radio-button label="-1">全部</el-radio-button>
            <el-radio-button label="3">未参加</el-radio-button>
            <el-radio-button label="2">补考</el-radio-button>
            <el-radio-button label="1">及格</el-radio-button>
            <el-radio-button label="0">未及格</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <!--            v-show="otherData.scoreType == '-1'"-->
      <el-col :span="6" v-if="scoreType">
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-download"
            @click="handleDownload()"
          >导出</el-button>
        </el-form-item>
      </el-col>
      <el-col :span="6"></el-col>
      <el-col :span="6">
        <el-form-item style="display:flex;justify-content: flex-end">
          <el-button type="small" @click="search(1)">搜索</el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
import { treeDataTranslate } from '@/utils'

export default {
  name: 'search-form',
  props: {
    examId: String,
    isType: {
      type: Boolean,
      default: false
    },
    scoreType: {
      type: Boolean,
      default: false
    },
    examType: {
      type: Boolean,
      default: false
    },
    setData: {
      type: Function,
      default() {
        return _ => ({})
      }
    },
    setLoading: {
      type: Function,
      default() {
        return _ => ({})
      }
    },
    setPage: {
      type: Function,
      default() {
        return _ => ({})
      }
    },
    setTotalPage: {
      type: Function,
      default() {
        return _ => ({})
      }
    },
    pagination: {
      type: Object,
      default: {
        page: 1,
        limit: 10
      }
    },
    url: Object
  },
  data() {
    return {
      dutyLoading: false,
      dataForm: {
        type: 'DUTY',
        deptId: '',
        realname: '',
        gender: '',
        identityCard: '',
        mobile: '',
        age: '',
        duty: '',
        education: '',
        jobNumber: '',
        professionalTitle: '',
        politicCountenance: '',
        craftName: '',
        courseExamStatus: 0
      },
      otherData: {
        scoreType: '-1'
      },
      dutyList: [],
      educationList: [],
      politicCountenanceList: [],
      professionalTitleList: [],
      deptListTreeProps: {
        checkStrictly: true,
        expandTrigger: 'hover',
        label: 'name',
        value: 'id',
        children: 'children',
        emitPath: false
      },
      deptList: [],
      typeworkList: [],
      deptName: '',
      userIds: []
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    handleDownload() {
      window.location = this.$http.downloadUrl(
        '/exam-admin/sta/exportexamusersta',
        {
          examId: this.examId,
          pass: this.otherData.scoreType
        }
      )
    },
    // obj传递备选参数
    init(obj) {
      this.dataForm = {
        type: 'DUTY',
        deptId: '',
        realname: '',
        gender: '',
        identityCard: '',
        mobile: '',
        age: '',
        duty: '',
        education: '',
        jobNumber: '',
        professionalTitle: '',
        politicCountenance: '',
        craftName: '',
        courseExamStatus: 0
      }
      this.otherData.scoreType = '-1'
      this.getDutyList()
      this.getDeptList()
      this.getTypeworkList()
      this.search(obj)
    },
    search(obj = {}) {
      // obj传递备选参数
      this.setLoading(true)
      this.userIds = []
      let a = true
      if (a) {
        if (this.examId) {
          if (
            [
              'mobile',
              'deptId',
              'age',
              'realname',
              'education',
              'gender',
              'jobNumber',
              'identityCard',
              'professionalTitle',
              'duty',
              'politicCountenance',
              'craftName'
            ]
              .map(x => this.dataForm[x])
              .filter(x => x !== '' && x !== null).length
          ) {
            this.$http({
              url: this.$http.adornUrl(this.url.getList),
              method: 'post',
              data: Object.assign(this.dataForm, this.pagination)
            })
              .then(res => {
                this.userIds = res.data.map(x => x.userId)
                if (this.userIds.length) {
                  this.getscores(1)
                } else {
                  this.setData([])
                }
                this.setLoading(false)
              })
              .catch(_ => {
                this.setLoading(false)
              })
          } else {
            this.getscores(1)
            this.setLoading(false)
          }
        } else {
          this.$http({
            url: this.$http.adornUrl(this.url.getList),
            method: 'post',
            data: Object.assign(this.dataForm, this.pagination)
          })
            .then(res => {
              this.setLoading(false)
              if (res.data.list.length) {
                this.setData(res.data.list, this.dataForm)
              } else {
                if (res.data.currPage > 1) {
                  let page = res.data.currPage - 1
                  this.setPage(page)
                } else {
                  this.setData(res.data.list)
                }
              }
              this.setTotalPage(res.data.totalCount)
            })
            .catch(_ => {
              this.setLoading(false)
            })
        }
      }
    },
    // 请求考试成绩
    getscores(page) {
      if (this.url.getscores) {
        this.$http({
          url: this.$http.adornUrl(this.url.getscores),
          method: 'post',
          data: Object.assign(
            {
              userIds: this.userIds.join(','),
              examId: this.examId,
              pass: this.otherData.scoreType
            },
            this.pagination,
            this.otherData
          )
        })
          .then(res => {
            this.setLoading(false)
            this.setData(res.data.list)
            this.setTotalPage(res.data.totalCount)
          })
          .catch(_ => {
            this.setLoading(false)
          })
      } else {
        this.setLoading(false)
      }
    },
    // 获取职务列表
    async getDutyList() {
      this.dutyLoading = true
      let list = await this.$http({
        url: this.$http.adornUrl(
          '/usercenter-admin/dict/getDictItemGroupByTypeList'
        ),
        method: 'post'
      })
      this.dutyLoading = false
      this.dutyList = list.data.dutyList
      this.educationList = list.data.educationList
      this.politicCountenanceList = list.data.politicCountenanceList
      this.professionalTitleList = list.data.professionalTitleList
    },
    // 获取部门列表
    async getDeptList() {
      let res = await this.$http({
        url: this.$http.adornUrl('/usercenter-admin/dept/list'),
        method: 'post'
      })
      this.deptList = treeDataTranslate(
        res.data.filter(item => item.parentId !== -1),
        'id'
      )
    },
    // 获取工种列表
    async getTypeworkList() {
      let res = await this.$http({
        url: this.$http.adornUrl(`/usercenter-admin/user/craft/list`),
        method: 'post'
      })
      this.typeworkList = res.data
    },
    // 菜单树选中
    deptListTreeCurrentChangeHandle(data, node) {
      this.deptName = data.name
      this.dataForm.deptId = data.id
    }
  }
}
</script>

<style scoped>
</style>
